<template>
  <el-dialog
    :append-to-body="true"
    :before-close="cancel"
    :close-on-click-modal="false"
    :visible.sync="isDialog"
    :title="isAdd ? '新增科室/房间' : '编辑科室/房间'"
    width="600px"
  >
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      size="small"
      label-width="120px"
    >
      <el-form-item label="定位点id" prop="pointId">
        <el-input v-model="ruleForm.pointId" type="number" style="width: 300px" />
      </el-form-item>
      <el-form-item label="医院" prop="locationId">
        <el-select v-model="ruleForm.locationId" placeholder="请选择" style="width: 300px" >
          <el-option
            v-for="item in hospitals"
            :key="item.locationId"
            :label="item.name"
            :value="item.locationId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="building">
        <el-input v-model="ruleForm.building"  style="width: 300px" />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input v-model="ruleForm.floor"  style="width: 300px" />
      </el-form-item>
      <el-form-item label="科室/房间" prop="showName">
        <el-input v-model="ruleForm.showName"  style="width: 300px" />
      </el-form-item>
      <el-form-item label="关键字" prop="pointName">
        <el-input v-model="ruleForm.pointName" style="width: 300px" />
      </el-form-item>
      <el-form-item label="科室照片">
        <el-upload
          action="#"
          class="avatar-uploader"
          :show-file-list="false"
          accept="image/*"
          :auto-upload="false"
          :on-change="imagesChange"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" size="mini" type="primary" @click="submit()"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import departmentApi from '@/network/map/department.api'
import {mapGetters,mapActions, createLogger} from 'vuex'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      isDialog: false,
      loading: false,
      imageUrl: '',
      imgDescList: [],
      locationIdOld: '',
      ruleForm: {
        pointId: '',
        building: '',
        floor: '',
        showName: '',
        pointName: '',
        locationId: '',
        pointImage: '',
        image: null
      },
      rules: {
        pointId: [{ required: true, message: '请输入定位点id', trigger: 'blur' }],
        building: [{ required: true, message: '请输入楼栋', trigger: 'blur' }],
        floor: [{ required: true, message: '请输入楼层', trigger: 'blur' }],
        showName: [{ required: true, message: '请输入前端显示名字', trigger: 'blur' }],
        pointName: [{ required: true, message: '请输入科室房间', trigger: 'blur' }],
        locationId: [
          { required: true, message: '请选择医院', trigger: 'change' },
        ],
      },
    }
  },
  methods: {
    ...mapActions('hospital',['saveHospitals']),
    imagesChange(file) { 
      this.ruleForm.image = file.raw
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          let data = {...this.ruleForm}
          if(this.locationIdOld !== '') {
            data.locationIdOld = this.locationIdOld
          } 
          if(data.image  && data.imgUrl) {
            data.pointImage =`${data.pointImage.replace(data.imgUrl,'')};#;`
          }
          const fdata = new FormData()
          for(const key in data) {
            key != 'imgUrl' && fdata.append(key,data[key])
          }
          let fun = this.isAdd ? 'addPoint' : 'updatePoint'
          departmentApi[fun](fdata).then(() => {
            let message = this.isAdd ? '添加成功！' : '修改成功！'
            this.saveHospitals()
            this.$message({message: message, type: 'success'})
            this.cancel()
          }).catch(err => {
            console.log(err);
            this.loading = false
          })
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.isDialog = false
      this.loading = false
      this.locationIdOld = ''
      this.imageUrl = '',
      this.ruleForm = {
        pointId: '',
        building: '',
        floor: '',
        showName: '',
        pointName: '',
        locationId: '',
        pointImage: '',
        image: null
      }
      this.$emit('refresh')
    },
  },
  computed: {
    ...mapGetters(['hospitals'])
  }
}
</script>
<style lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 0.5px #8c939d dashed;
    // border-style: dashed;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
